package com.suncnpap.journal.controller;

import com.suncnpap.journal.model.Journal;
import com.suncnpap.journal.model.JournalRegistration;
import com.suncnpap.journal.service.JournalRegistrationService;
import com.suncnpap.journal.service.JournalService;
import com.suncnpap.reader.model.Reader;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.app.util.common.HandlerResult;
import com.app.util.common.ObjUtil;
import com.app.util.common.ResponseUtil;
import com.app.util.common.UuidUtil;
import com.app.util.database.mybatis.Page;
import com.app.util.database.mybatis.QueryType;
import com.suncnpap.base.common.GlobaUser;
import com.suncnpap.base.common.ParamModel;
import com.suncnpap.base.controller.BaseController;
import com.suncnpap.datamanage.common.ExportExcelUtils;

/** 
 * 说明：期刊Controller
 */
@Controller
@RequestMapping(value="/journal/journal")
public class JournalController extends BaseController {
	
	@Autowired
	JournalService journalService;
	
	@Autowired
	JournalRegistrationService journalRegistrationService;
	
	
	@RequestMapping(value="/list")
	public void list(HttpServletRequest request, HttpServletResponse response) throws Exception{
		
		HandlerResult result = this.checkToken(request);
		
		if (result.getCode()==0) {

			Page page = this.setPage();
			
			String dtStartCreatDate = this.getParameter("dtStartCreatDate", "");
			if(!dtStartCreatDate.equals("")){
				page.addQueryAnd("dtCreatDate", QueryType.markGTEQ, dtStartCreatDate);
			}
			
			String dtEndCreatDate = this.getParameter("dtEndCreatDate", "");
			if(!dtEndCreatDate.equals("")){
				page.addQueryAnd("dtCreatDate", QueryType.markLTEQ, dtEndCreatDate);
			}
			
			Integer intLogOff = Integer.valueOf(this.getParameter("intLogOff", "2"));
			if(intLogOff!=2){
				page.addQueryAnd("intLogOff", intLogOff);
			}
			
			String strPostcode = this.getParameter("strPostcode", "");
			if(!strPostcode.equals("")){
				page.addQueryAnd("strPostcode", strPostcode);
			}
			page = journalService.list(page);
			
			String isExp = this.getParameter("isExp", "");
			if(isExp.equals("1")){
				List<JournalRegistration> libraryList = page.getDataList();
				String header = "期刊名,所属年度,邮发代号,CN,ISSN,出版单位,操作员,订阅时间,是否注销";
				
				String tableStr = "strJournalTitle,strYear,strPostcode,strUnifiedIssue,strIssue,strEditorialDepartment,"
						+ "strCreatName,dtCreatDate,intLogOff";
				
				ExportExcelUtils util = new ExportExcelUtils();
				String path = "upload/exp/";
				String fileName = UuidUtil.get32UUID()+".xls";
				util.exportExcel("期刊订阅", ObjUtil.strToList(header), ObjUtil.strToList(tableStr),libraryList, path, fileName);
				
				result.put("path", path);
				result.put("fileName", fileName);
			}else{
				
				Page registrationPage = new Page();
				registrationPage.addQueryAnd("r.strSchoolId", GlobaUser.getStrSchoolId(request.getHeader("token")));
				List<JournalRegistration> tempList = journalRegistrationService.list(registrationPage).getDataList();
				Map<String, List<JournalRegistration>> map = ObjUtil.listToMapByField(tempList, "strJournalId");
				
				List<Journal> list = page.getDataList();
				for(Journal journal:list){
					journal.setIntIsRegistration(map.get(journal.getStrId())!=null&&map.get(journal.getStrId()).size()>0?1:0);
				}
				result = this.returnPage(result, page);
			}
		}
		
		ResponseUtil.writeJson(result, response);

	}
	
	/**
	 * 添加处理
	 * @param obj
	 * @param response
	 */
	@RequestMapping(value="/add",method=RequestMethod.POST)
	public void add(@RequestBody Journal obj,HttpServletRequest request, HttpServletResponse response) throws Exception{
		
		HandlerResult result = this.checkToken(request);
		if (result.getCode()==0) {
			try{
				super.init(obj);
				obj.setIntLogOff(0);//未注销
				obj.setDtStorageTime(new Date());
				journalService.add(obj);
			}catch(Exception e){
				result.setCode(1);
				e.printStackTrace();
				throw e;
			}
		}
		ResponseUtil.writeJson(result, response);
	}
	
	
	@RequestMapping(value="/update",method=RequestMethod.POST)
	public void update(@RequestBody Journal obj,HttpServletRequest request, HttpServletResponse response) throws Exception{
		
		HandlerResult result = this.checkToken(request);
		if (result.getCode()==0) {
			try{
				journalService.update(obj);
			}catch(Exception e){
				result.setCode(1);
				e.printStackTrace();
				throw e;
			}
		}
		ResponseUtil.writeJson(result, response);
	}
	
	/**
	 * 删除处理
	 * @param strId
	 * @return
	 */
	@RequestMapping(value="/delete",method=RequestMethod.POST)
	public void delete(@RequestBody ParamModel paramModel,HttpServletRequest request, HttpServletResponse response) throws Exception{
		
		HandlerResult result = this.checkToken(request);
		if (result.getCode()==0) {
			try{
				String strId = paramModel.getStrId();
				if(strId != null && !strId.equals("")){
					journalService.deleteMore(strId.split(","));
				}
			}catch(Exception e){
				result.setCode(1);
				e.printStackTrace();
				throw e;
			}
		}
		ResponseUtil.writeJson(result, response);
	}
	
	
	@RequestMapping(value="/view")
	public void view(@RequestBody ParamModel paramModel,HttpServletRequest request, HttpServletResponse response) throws Exception{
		
		HandlerResult result = this.checkToken(request);
		if (result.getCode()==0) {
			try{
				String strId = paramModel.getStrId();
				Journal obj = journalService.findById(strId);
				result.put("obj", obj);
			}catch(Exception e){
				result.setCode(1);
				e.printStackTrace();
				throw e;
			}
		}
		ResponseUtil.writeJson(result, response);
	}
	
	/**
	 * 挂失处理
	 * @param strId
	 * @return
	 */
	@RequestMapping(value="/setLogOff",method=RequestMethod.POST)
	public void setLogOff(@RequestBody Journal journal,HttpServletRequest request,HttpServletResponse response) throws Exception{
		
		HandlerResult result = this.checkToken(request);
		if (result.getCode()==0) {
			try{
				journalService.setLogOff(journal.getStrId(),journal.getIntLogOff(), result.get("strSchoolId").toString());
			}catch(Exception e){
				result.setCode(1);
				e.printStackTrace();
				throw e;
			}
		}
		ResponseUtil.writeJson(result, response);
	}
}
